<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100">
      <el-form-item label="作品名称" prop="title">
        <el-input
            v-model="queryParams.title"
            placeholder="请输入作品名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作品介绍" prop="description">
        <el-input
            v-model="queryParams.description"
            placeholder="请输入作品介绍"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="作品作者" prop="userId">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.userId"-->
      <!--            placeholder="请输入作品作者"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="作品所属比赛" prop="competitionId">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.competitionId"-->
      <!--            placeholder="请输入作品所属比赛"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option
              v-for="dict in business_works_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="失败原因" prop="reason">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.reason"-->
      <!--            placeholder="请输入失败原因"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="Plus"-->
<!--            @click="handleAdd"-->
<!--            v-hasPermi="['business:works:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="success"-->
<!--            plain-->
<!--            icon="Edit"-->
<!--            :disabled="single"-->
<!--            @click="handleUpdate"-->
<!--            v-hasPermi="['business:works:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="Delete"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['business:works:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="warning"-->
<!--            plain-->
<!--            icon="Download"-->
<!--            @click="handleExport"-->
<!--            v-hasPermi="['business:works:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="worksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="主键" align="center" prop="worksId"/>-->
      <el-table-column label="作品名称" align="center" prop="title"/>
      <el-table-column label="作品介绍" align="center" prop="description"/>
      <el-table-column label="作品附件" align="center" prop="attachments">
        <template #default="{row}">
          <el-link :underline="false" v-for="file in row?.attachments?.split(',')||[]" :key="file"
                   :href="`/api${file}`">
            {{ file.slice(file.lastIndexOf('/') + 1) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="作品作者" align="center" prop="user.nickName"/>
      <el-table-column label="审核状态" align="center" prop="status">
        <template #default="{row}">
          <dict-tag :options="business_works_status" :value="row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="原因" align="center" prop="reason">
        <template #default="{row}">
          <span v-if="row.reason" style="color: #E65D6E">{{ row.reason }}</span>
          <span v-else>/</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{row}">
          <el-button v-if="row.status===0" link type="warning" icon="Finished" @click="handleAudit(row)"
                     v-hasPermi="['business:works:edit']">审核
          </el-button>
<!--          <el-button v-if="row.status!==1" link type="primary" icon="Edit" @click="handleUpdate(row)"-->
<!--                     v-hasPermi="['business:works:edit']">修改-->
<!--          </el-button>-->
<!--          <el-button link type="danger" icon="Delete" @click="handleDelete(row)"-->
<!--                     v-hasPermi="['business:works:remove']">删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改比赛作品对话框 -->
    <el-dialog :title="title" v-model="open" width="70%" append-to-body>
      <el-form ref="worksRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="作品名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入作品名称"/>
        </el-form-item>
        <el-form-item label="作品介绍" prop="description">
          <el-input type="textarea" v-model="form.description" placeholder="请输入作品介绍"/>
        </el-form-item>
        <el-form-item label="作品附件" prop="attachments">
          <file-upload v-model="form.attachments"/>
        </el-form-item>
        <!--        <el-form-item label="审核状态" prop="status">-->
        <!--          <el-select class="ff" v-model="form.status" placeholder="请选择审核状态">-->
        <!--            <el-option-->
        <!--                v-for="dict in business_works_status"-->
        <!--                :key="dict.value"-->
        <!--                :label="dict.label"-->
        <!--                :value="parseInt(dict.value)"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="失败原因" prop="reason">-->
        <!--          <el-input v-model="form.reason" placeholder="请输入失败原因"/>-->
        <!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


<!--    审核作品-->
    <el-dialog :title="title" v-model="auditOpen" width="70%" append-to-body>
      <el-form ref="worksRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="审核状态" prop="status">
          <el-select class="ff" v-model="form.status" placeholder="请选择审核状态">
            <el-option
                v-for="dict in business_works_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.status===2" label="失败原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入失败原因"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Works">
import {addWorks, delWorks, getWorks, listWorks, updateWorks} from "@/api/business/works";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";
import {useRoute} from "vue-router";

const {proxy} = getCurrentInstance();
const {business_works_status} = proxy.useDict('business_works_status');

const worksList = ref([]);
const open = ref(false);
const auditOpen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const route = useRoute();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    description: null,
    attachments: null,
    userId: null,
    competitionId: null,
    status: null,
    reason: null
  },
  rules: {
    title: [
      {required: true, message: "作品名称不能为空", trigger: "blur"}
    ],
    description: [
      {required: true, message: "作品介绍不能为空", trigger: "blur"}
    ],
    attachments: [
      {required: true, message: "作品附件不能为空", trigger: "blur"}
    ],
    status: [
      {required: true, message: "审核状态不能为空", trigger: "change"}
    ],
    reason: [
      {required: true, message: "审核不通过的原因不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询比赛作品列表 */
function getList() {
  loading.value = true;
  const competitionId = route.params.competitionId
  /*获取全部*/
  if (!competitionId) {
    listWorks(queryParams.value).then(response => {
      worksList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  } else {
    /*获取指定比赛下的作品*/
    queryParams.value.competitionId = competitionId
    listWorks(queryParams.value).then(response => {
      worksList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

}

// 取消按钮
function cancel() {
  open.value = false;
  auditOpen.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    worksId: null,
    title: null,
    description: null,
    attachments: null,
    userId: null,
    competitionId: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    status: null,
    reason: null
  };
  proxy.resetForm("worksRef");
  form.value.competitionId = route.params?.competitionId
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.worksId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加比赛作品";
}

/**审核作品*/
function handleAudit(row) {
  reset();
  const _worksId = row.worksId || ids.value
  getWorks(_worksId).then(response => {
    form.value = response.data;
    auditOpen.value = true;
    title.value = "审核作品";
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _worksId = row.worksId || ids.value
  getWorks(_worksId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改比赛作品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["worksRef"].validate(valid => {
    if (valid) {
      if (form.value.worksId != null) {
        updateWorks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          auditOpen.value = false;
          getList();
        });
      } else {
        addWorks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          auditOpen.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _worksIds = row.worksId || ids.value;
  proxy.$modal.confirm('是否确认删除比赛作品编号为"' + _worksIds + '"的数据项？').then(function () {
    return delWorks(_worksIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/works/export', {
    ...queryParams.value
  }, `works_${new Date().getTime()}.xlsx`)
}

getList();
</script>
